package com.spider.gp.ai;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.LowHigh;
import com.spider.gp.dao.LowHighDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;

/**
 * description:
 *  1.前后10个交易日都在最低点的上方则它是短期的最低点。
 *  2.前后10个交易日都在最高点的下方则它是短期的最高点。
 * 分析：
 * 1.最高点与它相邻的两个最低点形成一条直线，该最高点与下一个最高点形成一条直线。
 *
 * @author clare
 * @date 2022/05/20 09:04
 **/
@RestController
@Slf4j
public class HighLowAIController {

    @Autowired
    private LowHighDao lowHighDao;

    public static void main(String[] args) {
        //2.0221223E7|22.94|2.022111E7|23.22
        //-0.28x-113y+5664534.66=0
        //2.0221215E7|25.4|2.0221115E7|25.74
        //-0.34x-100y+6877753.10=0

        //25.0|22.94|4.0|23.22
        //-0.28x-21.0y+488.740=0
        //32.0|25.4|1.0|25.74
        //-0.34x-31.0y+798.280=0
        double[] point1= new double[]{32,22.94};
        double[] point2 = new double[]{1,23.22};//1110

        double[] point3 = new double[]{25,25.4};//1215
        double[] point4 = new double[]{4,25.74};//1115
        double[] node = cross(point1,point2,point3,point4);
        if(node==null)System.out.println(false);
        else System.out.println(node[0]+","+node[1]);
        HashSet<String> set = new HashSet<>();
        set.add(node[0]+":"+node[1]);
        for(String item:set){
            System.out.println(item);
        }

        //2.0221938532649625E7:22.939988991268265
    }

    public static double[] cross(double[] point1,double[] point2,double[] point3,double[] point4){
        if((point2[1]-point1[1])*(point4[0]-point3[0])==(point4[1]-point3[1])*(point2[0]-point1[0]))return null;
        if(point2[0]-point1[0]==0){
            double k2 = (point4[1]-point3[1])/(point4[0]-point3[0]);
            double b2 = point3[1]-point3[0]*k2;
            double cross_x = point1[0];
            double cross_y = k2*cross_x+b2;
            return new double[]{cross_x,cross_y};
        }
        if(point4[0]-point3[0]==0){
            double k1 = (point2[1]-point1[1])/(point2[0]-point1[0]);
            double b1 = point1[1]-point1[0]*k1;
            double cross_x = point3[0];
            double cross_y = k1*cross_x+b1;
            return new double[]{cross_x,cross_y};
        }
        double k1 = (point2[1]-point1[1])/(point2[0]-point1[0]);
        double b1 = point1[1]-point1[0]*k1;
        double k2 = (point4[1]-point3[1])/(point4[0]-point3[0]);
        double b2 = point3[1]-point3[0]*k2;
        double cross_x = (b1-b2)/(k2-k1);
        double cross_y = k1*cross_x+b1;
        return new double[]{cross_x,cross_y};
    }


    // 两点坐标形成一条直线
    public void test(){

        LambdaQueryWrapper<LowHigh> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LowHigh::getCode , "600584");
        LowHigh lowHigh = lowHighDao.selectOne(wrapper);

        String lowPoint = lowHigh.getLowPoint();
        String[] split = lowPoint.split(";");
        String dsdy = split[split.length - 1];
        String  dsde = split[split.length - 2];
        String[] s1 = dsdy.split(",");
        String[] d1 = dsde.split(",");
        //把A(a,b),B（c,d）带入得：  (b-d)x-(a-c)y+ad-bc=0
        Double a = Double.valueOf(25);
        Double b = Double.valueOf(s1[1]);
        Double c = Double.valueOf(4);
        Double d = Double.valueOf(d1[1]);
        System.out.println(a+"|"+b+"|"+c+"|"+d);
        BigDecimal x = BigDecimal.valueOf(b).subtract(BigDecimal.valueOf(d));
        BigDecimal y = BigDecimal.valueOf(a).subtract(BigDecimal.valueOf(c));
        BigDecimal cs = BigDecimal.valueOf(a).multiply( BigDecimal.valueOf(d)).subtract(BigDecimal.valueOf(b).multiply( BigDecimal.valueOf(c)));
        System.out.println(x+"x"+"-"+y+"y+"+cs+"=0");//-0.28x-113y+5664534.66=0
        //-0.28x-113y+5664534.66=0
        //-0.34x-100y+6877753.10=0
    }

    public void test2(){

        LambdaQueryWrapper<LowHigh> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LowHigh::getCode , "600584");
        LowHigh lowHigh = lowHighDao.selectOne(wrapper);

        String highPoint = lowHigh.getHighPoint();
        String[] split = highPoint.split(";");
        String dsdy = split[split.length - 1];
        String  dsde = split[split.length - 2];
        String[] s1 = dsdy.split(",");
        String[] d1 = dsde.split(",");
        //把A(a,b),B（c,d）带入得：  (b-d)x-(a-c)y+ad-bc=0
        Double a = Double.valueOf(32);
        Double b = Double.valueOf(s1[1]);
        Double c = Double.valueOf(1);
        Double d = Double.valueOf(d1[1]);
        System.out.println(a+"|"+b+"|"+c+"|"+d);

        BigDecimal x = BigDecimal.valueOf(b).subtract(BigDecimal.valueOf(d));
        BigDecimal y = BigDecimal.valueOf(a).subtract(BigDecimal.valueOf(c));
        BigDecimal cs = BigDecimal.valueOf(a).multiply( BigDecimal.valueOf(d)).subtract(BigDecimal.valueOf(b).multiply( BigDecimal.valueOf(c)));
        System.out.println(x+"x"+"-"+y+"y+"+cs+"=0");//-0.28x-113y+5664534.66=0

    }
}
